package com.chidopi.market.bookstore.service.impl;

import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.chidopi.cdf.dao.IBaseDao;
import com.chidopi.cdf.exception.CdfException;
import com.chidopi.cdf.service.IBaseService;
import com.chidopi.market.bookstore.domain.BookCategories;
import com.chidopi.market.bookstore.domain.City2Category;

public class BookCategoriesServiceImpl implements IBaseService<BookCategories> {
	
	private static Logger logger = LoggerFactory.getLogger(BookCategoriesServiceImpl.class);
	
	@Autowired
	@Qualifier("bookCategoriesDao")
	private IBaseDao<BookCategories> baseDao;
	
	@Autowired
	@Qualifier("city2categoryDao")
	private IBaseDao<City2Category> city2categoryDao;
	

	
	@Override
	public int insert(BookCategories t) throws Exception {
		return this.baseDao.insert(t);
	}

	@Override
	public int update(BookCategories t) throws Exception {
		return this.baseDao.update(t);
	}

	@Override
	public int delete(BookCategories t) throws Exception {
		Integer id = t.getId();
		
		String sql = "id in (select CATEGORY_ID from book2category where CATEGORY_ID = ? ) ";
		List<BookCategories> categories = baseDao.find(sql, id);		
		if(categories != null && categories.size() > 0){
			logger.debug("delete [" + id + "] failure: BookCategories.size()=" + categories.size());
			throw new CdfException("error.bookcategory.delete.book2category");
		}
		
		List<City2Category> cc = this.city2categoryDao.find(" a.CATEGORY_ID = ? OR a.PARENT_ID = ? ", id ,id);
		if(cc != null && cc.size() > 0){
			logger.debug("delete [" + id + "] failure: City2Category.size()=" + cc.size());
			throw new CdfException("error.bookcategory.delete.city2category");
		}
		
		
		return this.baseDao.delete(t);
	}

	@Override
	public List<BookCategories> find(String sqlWhere, Object... args) throws Exception {
		logger.debug(sqlWhere);
		logger.debug(Arrays.asList(args).toString());
		return this.baseDao.find(sqlWhere, args);
	}

	@Override
	public BookCategories findById(Object id) throws Exception {
		return this.baseDao.findById(id);
	}

	@Override
	public int getNextSequence() throws Exception {
		return 0;
	}

}
